package com.shengzai.mapreduce.sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Student implements WritableComparable<Student> {
    String studentID;
    String name;
    int score;

    String clazz;

    int age;

    @Override
    public String toString() {
        return "Student{" +
                "studentID='" + studentID + '\'' +
                ", name='" + name + '\'' +
                ", score=" + score +
                ", clazz='" + clazz + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String studentID, String name, int score, String clazz, int age) {
        this.studentID = studentID;
        this.name = name;
        this.score = score;
        this.clazz = clazz;
        this.age = age;
    }

    public Student() {
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 序列化需要实现的两个函数 write readFields
     * write 是将对象变成一个二进制的序列化
     * 注意 write 和 read 的顺序需要保持一致
     *
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(this.studentID);
        out.writeUTF(this.name);
        out.writeInt(this.score);
        out.writeUTF(this.clazz);
        out.writeInt(this.age);

    }

    /**
     * 反序列化，将传入过来数据包装成一个student对象
     *
     * @param in <code>DataInput</code> to deseriablize this object from.
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.studentID = in.readUTF();
        this.name = in.readUTF();
        this.score = in.readInt();
        this.clazz = in.readUTF();
        this.age = in.readInt();
    }


    /**
     * public int compareTo(IntWritable o) {
     * int thisValue = this.value;
     * int thatValue = o.value;
     * return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
     * }
     */
    @Override
    public int compareTo(Student o) {
        String thisClazz = this.clazz;
        String  thatClazz = o.clazz;
        return -(thisClazz.compareTo(thatClazz) <0 ? -1 : (thisClazz.equals(thatClazz) ? 0 : 1));
    }
}
